package com.navi.loaddata.loadService.impl.mesData;

import com.navi.loaddata.core.gpEntity.MProdD;
import com.navi.loaddata.core.gpEntity.MppLayoutPnlD;
import com.navi.loaddata.core.gpEntity.array.WppAdefectGlassF;
import com.navi.loaddata.core.gpEntity.cf.WppFdefectGlassF;
import com.navi.loaddata.core.repository.impl.GreenPlumRepositoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(transactionManager = "secondaryPlatformTransactionManager",
        rollbackFor = {RuntimeException.class, Exception.class})
public class MppLayoutPnlDService {

    @Autowired
    private GreenPlumRepositoryImpl dao2;

    public List<MppLayoutPnlD> getMppLayoutPnlDByPanelId(String glassId) {
        String prodId = null;
        //去adefectglass查prod
        glassId=glassId.substring(0,10);
        List<MppLayoutPnlD> layoutPnlDS = new ArrayList<>();
        if (glassId.startsWith("A")) {
            List<WppAdefectGlassF> list = dao2.list(WppAdefectGlassF.class, "FROM WppAdefectGlassF WHERE glassId = '" + glassId + "'");
            if (list == null || list.size() <= 0) {
                return null;
            }
            prodId = list.get(0).getProdId();
        } else if (glassId.startsWith("C")) {
            List<WppFdefectGlassF> list = dao2.list(WppFdefectGlassF.class, "FROM WppFdefectGlassF WHERE glassId = '" + glassId + "'");
            if (list == null || list.size() <= 0) {
                return null;
            }
            prodId = list.get(0).getProdId();
        }
        if (prodId == null) {
            return null;
        }
        //去prod查layout
        List<MProdD> prodDS = dao2.list(MProdD.class, "FROM MProdD WHERE prodId = '" + prodId + "' and validFlg = 'Y'");
        if (prodDS == null || prodDS.size() <= 0) {
            return null;
        }
        String layoutId = prodDS.get(0).getLayoutId();
        if (org.apache.commons.lang.StringUtils.isBlank(layoutId)) {
            return null;
        }
        //查详细
        layoutPnlDS = dao2.list(MppLayoutPnlD.class, "FROM MppLayoutPnlD WHERE layoutId = '" + layoutId + "'");
        if (layoutPnlDS.size() <= 0) {
            return null;
        }
        return layoutPnlDS;
    }
}
